package com.autumn.scheduler.api;

import java.io.Serializable;

import com.autumn.scheduler.api.constant.ApiConstants.QueueRunState;
import com.autumn.util.ExceptionUtils;
import com.autumn.util.StringUtils;

/**
 * 队列调度状态
 * 
 * @author 老码农 2018-03-26 23:40:13
 */
public class QueueSchedulerStatus implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 7911529601693843901L;
	private String jobExecuteId;
	/**
	 * {@link com.autumn.scheduler.api.constant.ApiConstants.QueueRunState}
	 */
	private Integer state;
	private String message;
	private Long beginTime;
	private Long endTime;

	public String getJobExecuteId() {
		return jobExecuteId;
	}

	public void setJobExecuteId(String jobExecuteId) {
		this.jobExecuteId = jobExecuteId;
	}

	/**
	 * 
	 * @return {@link com.autumn.scheduler.api.constant.ApiConstants.QueueRunState}
	 *
	 */
	public Integer getState() {
		return state;
	}

	/**
	 * 
	 * @param state
	 *           {@link com.autumn.scheduler.api.constant.ApiConstants.QueueRunState}
	 *
	 */
	public void setState(Integer state) {
		this.state = state;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Long getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(Long beginTime) {
		this.beginTime = beginTime;
	}

	public Long getEndTime() {
		return endTime;
	}

	public void setEndTime(Long endTime) {
		this.endTime = endTime;
	}
	
	/**
	 * 失败结果
	 * 
	 * @param eventData
	 * @param beginTime
	 * @param endTime
	 * @param message
	 * @return
	 *
	 */
	public static QueueSchedulerStatus failResult(QueueJobEventData eventData, long beginTime, long endTime,
			String message) {
		if (StringUtils.isNullOrBlank(message)) {
			message = "队列执行作业失败";
		}
		QueueSchedulerStatus result = createResult(eventData, beginTime, endTime, message);
		result.setState(QueueRunState.JOB_FAIL);
		return result;
	}

	/**
	 * 成功结果
	 * 
	 * @param eventData
	 * @param beginTime
	 * @param endTime
	 * @param message
	 * @return
	 *
	 */
	public static QueueSchedulerStatus successResult(QueueJobEventData eventData, long beginTime, long endTime,
			String message) {
		if (StringUtils.isNullOrBlank(message)) {
			message = "队列执行作业成功";
		}
		QueueSchedulerStatus result = createResult(eventData, beginTime, endTime, message);
		result.setState(QueueRunState.JOB_SUCCESS);
		return result;
	}

	/**
	 * 
	 * @param eventData
	 * @param beginTime
	 * @param endTime
	 * @param message
	 * @return
	 *
	 */
	private static QueueSchedulerStatus createResult(QueueJobEventData eventData, long beginTime, long endTime,
			String message) {
		ExceptionUtils.checkNotNull(eventData, "eventData");
		QueueSchedulerStatus result = new QueueSchedulerStatus();
		result.setJobExecuteId(eventData.getJobExecuteId());
		result.setMessage(message);
		result.setBeginTime(beginTime);
		result.setEndTime(endTime);
		return result;
	}
}
